package com.android.kuai.zhuan.utils;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.util.Log;
import android.widget.NumberPicker;

import com.android.kuai.zhuan.MyApplication;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

public class CommonUtils {

    /**
     * 获取app版本号
     *
     * @param context
     * @param pkgName
     * @return
     */
    public static String getPackageCode(Context context, String pkgName) {
        PackageManager manager = context.getPackageManager();
        String code = "";
        try {
            PackageInfo info = manager.getPackageInfo(pkgName, 0);
            code = info.versionName;
            Log.d("leihuiming", "version name = " + info.versionName);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return code;
    }

    /**
     * 获取手机Imei
     */
    public static String getPhoneImei() {
        TelephonyManager telManager = (TelephonyManager) MyApplication.context.getSystemService(Context.TELEPHONY_SERVICE);
        if (telManager != null) {
            String imei = telManager.getDeviceId();
            Log.d("leihuiming", "imei = " + imei);
            return imei;
        }
        return "";
    }

    public static List<String> RemoveDuplicates(List<String> stringList) {
        List<String> list = new ArrayList<>();
        list.addAll(stringList);
        for (int i = 0; i < list.size(); i++) {  //外循环是循环的次数
            for (int j = list.size() - 1; j > i; j--) {  //内循环是 外循环一次比较的次数
                if (list.get(i).equals(list.get(j))) {
                    list.remove(j);
                }
            }
        }
        return list;
    }

    /**
     * 获取脚本文件的文件名
     * @param pkgName
     * @return
     */
    public static String getScriptFileName(String pkgName) {
        StringBuffer buffer = new StringBuffer(pkgName);
        buffer.append("_");
        buffer.append(ScreenUtils.getScreenWidth(MyApplication.context));
        buffer.append(".");
        buffer.append(ScreenUtils.getScreenHeight(MyApplication.context));
        buffer.append(".txt");
        return buffer.toString();
    }

    /**
     * 判断app是否已安装
     * @param context
     * @param pkgName
     * @return
     */
    public static boolean appIsAvilible(Context context, String pkgName) {
        PackageManager pm = context.getPackageManager();
        Intent intent = pm.getLaunchIntentForPackage(pkgName);
        return intent != null;
    }

    public static void startApp(Context context, String pkgName) {
        PackageManager pm = context.getPackageManager();
        Intent intent = pm.getLaunchIntentForPackage(pkgName);
        if(intent != null) {
            context.startActivity(intent);
        }
    }

    /**
     * 设置picker分割线的颜色
     */
    public static void setDividerColor(NumberPicker picker) {
        try {
            Field field = NumberPicker.class.getDeclaredField("mSelectionDivider");
            if (field != null) {
                field.setAccessible(true);
                field.set(picker, new ColorDrawable(Color.RED));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 设置picker分割线的宽度
     */
    public static void setNumberPickerDivider(NumberPicker picker) {
        try {
            Field[] fields = NumberPicker.class.getDeclaredFields();
            for (Field f : fields) {
                if (f.getName().equals("mSelectionDividerHeight")) {
                    f.setAccessible(true);
                    f.set(picker, 1);
                    break;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
